home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 1998 November / IRIX 6.5.2 Base Documentation November 1998.img / usr / share / catman / u_man / cat1 / perlbot.z / perlbot
Text File  |  1998-10-30  |  21KB  |  727 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlbot - Bag'o Object Tricks (the BOT)
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.      The following collection of tricks and hints is intended to whet curious
  13.      appetites about such things as the use of instance variables and the
  14.      mechanics of object and class relationships.  The reader is encouraged to
  15.      consult relevant textbooks for discussion of Object Oriented definitions
  16.      and methodology.  This is not intended as a tutorial for object-oriented
  17.      programming or as a comprehensive guide to Perl's object oriented
  18.      features, nor should it be construed as a style guide.
  19.  
  20.      The Perl motto still holds:  There's more than one way to do it.
  21.  
  22. OOOOOOOO SSSSCCCCAAAALLLLIIIINNNNGGGG TTTTIIIIPPPPSSSS
  23.      1    Do not attempt to verify the type of $self.  That'll break if the
  24.           class is inherited, when the type of $self is valid but its package
  25.           isn't what you expect.  See rule 5.
  26.  
  27.      2    If an object-oriented (OO) or indirect-object (IO) syntax was used,
  28.           then the object is probably the correct type and there's no need to
  29.           become paranoid about it.  Perl isn't a paranoid language anyway.
  30.           If people subvert the OO or IO syntax then they probably know what
  31.           they're doing and you should let them do it.  See rule 1.
  32.  
  33.      3    Use the two-argument form of _b_l_e_s_s().  Let a subclass use your
  34.           constructor.  See the section on _I_N_H_E_R_I_T_I_N_G _A _C_O_N_S_T_R_U_C_T_O_R.
  35.  
  36.      4    The subclass is allowed to know things about its immediate
  37.           superclass, the superclass is allowed to know nothing about a
  38.           subclass.
  39.  
  40.      5    Don't be trigger happy with inheritance.  A "using", "containing",
  41.           or "delegation" relationship (some sort of aggregation, at least) is
  42.           often more appropriate.  See the section on _O_B_J_E_C_T _R_E_L_A_T_I_O_N_S_H_I_P_S,
  43.           the section on _U_S_I_N_G _R_E_L_A_T_I_O_N_S_H_I_P _W_I_T_H _S_D_B_M, and the section on
  44.           _D_E_L_E_G_A_T_I_O_N.
  45.  
  46.      6    The object is the namespace.  Make package globals accessible via
  47.           the object.  This will remove the guess work about the symbol's home
  48.           package.  See the section on _C_L_A_S_S _C_O_N_T_E_X_T _A_N_D _T_H_E _O_B_J_E_C_T.
  49.  
  50.      7    IO syntax is certainly less noisy, but it is also prone to
  51.           ambiguities that can cause difficult-to-find bugs.  Allow people to
  52.           use the sure-thing OO syntax, even if you don't like it.
  53.  
  54.      8    Do not use function-call syntax on a method.  You're going to be
  55.           bitten someday.  Someone might move that method into a superclass
  56.           and your code will be broken.  On top of that you're feeding the
  57.           paranoia in rule 2.
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  71.  
  72.  
  73.  
  74.      9    Don't assume you know the home package of a method.  You're making
  75.           it difficult for someone to override that method.  See the section
  76.           on _T_H_I_N_K_I_N_G _O_F _C_O_D_E _R_E_U_S_E.
  77.  
  78. IIIINNNNSSSSTTTTAAAANNNNCCCCEEEE VVVVAAAARRRRIIIIAAAABBBBLLLLEEEESSSS
  79.      An anonymous array or anonymous hash can be used to hold instance
  80.      variables.  Named parameters are also demonstrated.
  81.  
  82.              package Foo;
  83.  
  84.              sub new {
  85.                      my $type = shift;
  86.                      my %params = @_;
  87.                      my $self = {};
  88.                      $self->{'High'} = $params{'High'};
  89.                      $self->{'Low'}  = $params{'Low'};
  90.                      bless $self, $type;
  91.              }
  92.  
  93.              package Bar;
  94.  
  95.              sub new {
  96.                      my $type = shift;
  97.                      my %params = @_;
  98.                      my $self = [];
  99.                      $self->[0] = $params{'Left'};
  100.                      $self->[1] = $params{'Right'};
  101.                      bless $self, $type;
  102.              }
  103.  
  104.              package main;
  105.  
  106.              $a = Foo->new( 'High' => 42, 'Low' => 11 );
  107.              print "High=$a->{'High'}\n";
  108.              print "Low=$a->{'Low'}\n";
  109.  
  110.              $b = Bar->new( 'Left' => 78, 'Right' => 40 );
  111.              print "Left=$b->[0]\n";
  112.              print "Right=$b->[1]\n";
  113.  
  114.  
  115. SSSSCCCCAAAALLLLAAAARRRR IIIINNNNSSSSTTTTAAAANNNNCCCCEEEE VVVVAAAARRRRIIIIAAAABBBBLLLLEEEESSSS
  116.      An anonymous scalar can be used when only one instance variable is
  117.      needed.
  118.  
  119.              package Foo;
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  137.  
  138.  
  139.  
  140.              sub new {
  141.                      my $type = shift;
  142.                      my $self;
  143.                      $self = shift;
  144.                      bless \$self, $type;
  145.              }
  146.  
  147.              package main;
  148.  
  149.              $a = Foo->new( 42 );
  150.              print "a=$$a\n";
  151.  
  152.  
  153. IIIINNNNSSSSTTTTAAAANNNNCCCCEEEE VVVVAAAARRRRIIIIAAAABBBBLLLLEEEE IIIINNNNHHHHEEEERRRRIIIITTTTAAAANNNNCCCCEEEE
  154.      This example demonstrates how one might inherit instance variables from a
  155.      superclass for inclusion in the new class.  This requires calling the
  156.      superclass's constructor and adding one's own instance variables to the
  157.      new object.
  158.  
  159.              package Bar;
  160.  
  161.              sub new {
  162.                      my $type = shift;
  163.                      my $self = {};
  164.                      $self->{'buz'} = 42;
  165.                      bless $self, $type;
  166.              }
  167.  
  168.              package Foo;
  169.              @ISA = qw( Bar );
  170.  
  171.              sub new {
  172.                      my $type = shift;
  173.                      my $self = Bar->new;
  174.                      $self->{'biz'} = 11;
  175.                      bless $self, $type;
  176.              }
  177.  
  178.              package main;
  179.  
  180.              $a = Foo->new;
  181.              print "buz = ", $a->{'buz'}, "\n";
  182.              print "biz = ", $a->{'biz'}, "\n";
  183.  
  184.  
  185. OOOOBBBBJJJJEEEECCCCTTTT RRRREEEELLLLAAAATTTTIIIIOOOONNNNSSSSHHHHIIIIPPPPSSSS
  186.      The following demonstrates how one might implement "containing" and
  187.      "using" relationships between objects.
  188.  
  189.              package Bar;
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  203.  
  204.  
  205.  
  206.              sub new {
  207.                      my $type = shift;
  208.                      my $self = {};
  209.                      $self->{'buz'} = 42;
  210.                      bless $self, $type;
  211.              }
  212.  
  213.              package Foo;
  214.  
  215.              sub new {
  216.                      my $type = shift;
  217.                      my $self = {};
  218.                      $self->{'Bar'} = Bar->new;
  219.                      $self->{'biz'} = 11;
  220.                      bless $self, $type;
  221.              }
  222.  
  223.              package main;
  224.  
  225.              $a = Foo->new;
  226.              print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
  227.              print "biz = ", $a->{'biz'}, "\n";
  228.  
  229.  
  230. OOOOVVVVEEEERRRRRRRRIIIIDDDDIIIINNNNGGGG SSSSUUUUPPPPEEEERRRRCCCCLLLLAAAASSSSSSSS MMMMEEEETTTTHHHHOOOODDDDSSSS
  231.      The following example demonstrates how to override a superclass method
  232.      and then call the overridden method.  The SSSSUUUUPPPPEEEERRRR pseudo-class allows the
  233.      programmer to call an overridden superclass method without actually
  234.      knowing where that method is defined.
  235.  
  236.              package Buz;
  237.              sub goo { print "here's the goo\n" }
  238.  
  239.              package Bar; @ISA = qw( Buz );
  240.              sub google { print "google here\n" }
  241.  
  242.              package Baz;
  243.              sub mumble { print "mumbling\n" }
  244.  
  245.              package Foo;
  246.              @ISA = qw( Bar Baz );
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  269.  
  270.  
  271.  
  272.              sub new {
  273.                      my $type = shift;
  274.                      bless [], $type;
  275.              }
  276.              sub grr { print "grumble\n" }
  277.              sub goo {
  278.                      my $self = shift;
  279.                      $self->SUPER::goo();
  280.              }
  281.              sub mumble {
  282.                      my $self = shift;
  283.                      $self->SUPER::mumble();
  284.              }
  285.              sub google {
  286.                      my $self = shift;
  287.                      $self->SUPER::google();
  288.              }
  289.  
  290.              package main;
  291.  
  292.              $foo = Foo->new;
  293.              $foo->mumble;
  294.              $foo->grr;
  295.              $foo->goo;
  296.              $foo->google;
  297.  
  298.  
  299. UUUUSSSSIIIINNNNGGGG RRRREEEELLLLAAAATTTTIIIIOOOONNNNSSSSHHHHIIIIPPPP WWWWIIIITTTTHHHH SSSSDDDDBBBBMMMM
  300.      This example demonstrates an interface for the SDBM class.  This creates
  301.      a "using" relationship between the SDBM class and the new class Mydbm.
  302.  
  303.              package Mydbm;
  304.  
  305.              require SDBM_File;
  306.              require Tie::Hash;
  307.              @ISA = qw( Tie::Hash );
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  335.  
  336.  
  337.  
  338.              sub TIEHASH {
  339.                  my $type = shift;
  340.                  my $ref  = SDBM_File->new(@_);
  341.                  bless {'dbm' => $ref}, $type;
  342.              }
  343.              sub FETCH {
  344.                  my $self = shift;
  345.                  my $ref  = $self->{'dbm'};
  346.                  $ref->FETCH(@_);
  347.              }
  348.              sub STORE {
  349.                  my $self = shift;
  350.                  if (defined $_[0]){
  351.                      my $ref = $self->{'dbm'};
  352.                      $ref->STORE(@_);
  353.                  } else {
  354.                      die "Cannot STORE an undefined key in Mydbm\n";
  355.                  }
  356.              }
  357.  
  358.              package main;
  359.              use Fcntl qw( O_RDWR O_CREAT );
  360.  
  361.              tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640;
  362.              $foo{'bar'} = 123;
  363.              print "foo-bar = $foo{'bar'}\n";
  364.  
  365.              tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640;
  366.              $bar{'Cathy'} = 456;
  367.              print "bar-Cathy = $bar{'Cathy'}\n";
  368.  
  369.  
  370. TTTTHHHHIIIINNNNKKKKIIIINNNNGGGG OOOOFFFF CCCCOOOODDDDEEEE RRRREEEEUUUUSSSSEEEE
  371.      One strength of Object-Oriented languages is the ease with which old code
  372.      can use new code.  The following examples will demonstrate first how one
  373.      can hinder code reuse and then how one can promote code reuse.
  374.  
  375.      This first example illustrates a class which uses a fully-qualified
  376.      method call to access the "private" method _B_A_Z().  The second example
  377.      will show that it is impossible to override the _B_A_Z() method.
  378.  
  379.              package FOO;
  380.  
  381.              sub new {
  382.                      my $type = shift;
  383.                      bless {}, $type;
  384.              }
  385.              sub bar {
  386.                      my $self = shift;
  387.                      $self->FOO::private::BAZ;
  388.              }
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  401.  
  402.  
  403.  
  404.              package FOO::private;
  405.  
  406.              sub BAZ {
  407.                      print "in BAZ\n";
  408.              }
  409.  
  410.              package main;
  411.  
  412.              $a = FOO->new;
  413.              $a->bar;
  414.  
  415.      Now we try to override the _B_A_Z() method.  We would like _F_O_O::_b_a_r() to
  416.      call _G_O_O_P::_B_A_Z(), but this cannot happen because _F_O_O::_b_a_r() explicitly
  417.      calls _F_O_O::_p_r_i_v_a_t_e::_B_A_Z().
  418.  
  419.              package FOO;
  420.  
  421.              sub new {
  422.                      my $type = shift;
  423.                      bless {}, $type;
  424.              }
  425.              sub bar {
  426.                      my $self = shift;
  427.                      $self->FOO::private::BAZ;
  428.              }
  429.  
  430.              package FOO::private;
  431.  
  432.              sub BAZ {
  433.                      print "in BAZ\n";
  434.              }
  435.  
  436.              package GOOP;
  437.              @ISA = qw( FOO );
  438.              sub new {
  439.                      my $type = shift;
  440.                      bless {}, $type;
  441.              }
  442.  
  443.              sub BAZ {
  444.                      print "in GOOP::BAZ\n";
  445.              }
  446.  
  447.              package main;
  448.  
  449.              $a = GOOP->new;
  450.              $a->bar;
  451.  
  452.      To create reusable code we must modify class FOO, flattening class
  453.      FOO::private.  The next example shows a reusable class FOO which allows
  454.      the method _G_O_O_P::_B_A_Z() to be used in place of _F_O_O::_B_A_Z().
  455.  
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  467.  
  468.  
  469.  
  470.              package FOO;
  471.  
  472.              sub new {
  473.                      my $type = shift;
  474.                      bless {}, $type;
  475.              }
  476.              sub bar {
  477.                      my $self = shift;
  478.                      $self->BAZ;
  479.              }
  480.  
  481.              sub BAZ {
  482.                      print "in BAZ\n";
  483.              }
  484.  
  485.              package GOOP;
  486.              @ISA = qw( FOO );
  487.  
  488.              sub new {
  489.                      my $type = shift;
  490.                      bless {}, $type;
  491.              }
  492.              sub BAZ {
  493.                      print "in GOOP::BAZ\n";
  494.              }
  495.  
  496.              package main;
  497.  
  498.              $a = GOOP->new;
  499.              $a->bar;
  500.  
  501.  
  502. CCCCLLLLAAAASSSSSSSS CCCCOOOONNNNTTTTEEEEXXXXTTTT AAAANNNNDDDD TTTTHHHHEEEE OOOOBBBBJJJJEEEECCCCTTTT
  503.      Use the object to solve package and class context problems.  Everything a
  504.      method needs should be available via the object or should be passed as a
  505.      parameter to the method.
  506.  
  507.      A class will sometimes have static or global data to be used by the
  508.      methods.  A subclass may want to override that data and replace it with
  509.      new data.  When this happens the superclass may not know how to find the
  510.      new copy of the data.
  511.  
  512.      This problem can be solved by using the object to define the context of
  513.      the method.  Let the method look in the object for a reference to the
  514.      data.  The alternative is to force the method to go hunting for the data
  515.      ("Is it in my class, or in a subclass?  Which subclass?"), and this can
  516.      be inconvenient and will lead to hackery.  It is better just to let the
  517.      object tell the method where that data is located.
  518.  
  519.              package Bar;
  520.  
  521.              %fizzle = ( 'Password' => 'XYZZY' );
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  533.  
  534.  
  535.  
  536.              sub new {
  537.                      my $type = shift;
  538.                      my $self = {};
  539.                      $self->{'fizzle'} = \%fizzle;
  540.                      bless $self, $type;
  541.              }
  542.  
  543.              sub enter {
  544.                      my $self = shift;
  545.  
  546.                      # Don't try to guess if we should use %Bar::fizzle
  547.                      # or %Foo::fizzle.  The object already knows which
  548.                      # we should use, so just ask it.
  549.                      #
  550.                      my $fizzle = $self->{'fizzle'};
  551.  
  552.                      print "The word is ", $fizzle->{'Password'}, "\n";
  553.              }
  554.  
  555.              package Foo;
  556.              @ISA = qw( Bar );
  557.  
  558.              %fizzle = ( 'Password' => 'Rumple' );
  559.  
  560.              sub new {
  561.                      my $type = shift;
  562.                      my $self = Bar->new;
  563.                      $self->{'fizzle'} = \%fizzle;
  564.                      bless $self, $type;
  565.              }
  566.  
  567.              package main;
  568.  
  569.              $a = Bar->new;
  570.              $b = Foo->new;
  571.              $a->enter;
  572.              $b->enter;
  573.  
  574.  
  575. IIIINNNNHHHHEEEERRRRIIIITTTTIIIINNNNGGGG AAAA CCCCOOOONNNNSSSSTTTTRRRRUUUUCCCCTTTTOOOORRRR
  576.      An inheritable constructor should use the second form of _b_l_e_s_s() which
  577.      allows blessing directly into a specified class.  Notice in this example
  578.      that the object will be a BAR not a FOO, even though the constructor is
  579.      in class FOO.
  580.  
  581.              package FOO;
  582.  
  583.              sub new {
  584.                      my $type = shift;
  585.                      my $self = {};
  586.                      bless $self, $type;
  587.              }
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  599.  
  600.  
  601.  
  602.              sub baz {
  603.                      print "in FOO::baz()\n";
  604.              }
  605.  
  606.              package BAR;
  607.              @ISA = qw(FOO);
  608.  
  609.              sub baz {
  610.                      print "in BAR::baz()\n";
  611.              }
  612.  
  613.              package main;
  614.  
  615.              $a = BAR->new;
  616.              $a->baz;
  617.  
  618.  
  619. DDDDEEEELLLLEEEEGGGGAAAATTTTIIIIOOOONNNN
  620.      Some classes, such as SDBM_File, cannot be effectively subclassed because
  621.      they create foreign objects.  Such a class can be extended with some sort
  622.      of aggregation technique such as the "using" relationship mentioned
  623.      earlier or by delegation.
  624.  
  625.      The following example demonstrates delegation using an _A_U_T_O_L_O_A_D()
  626.      function to perform message-forwarding.  This will allow the Mydbm object
  627.      to behave exactly like an SDBM_File object.  The Mydbm class could now
  628.      extend the behavior by adding custom _F_E_T_C_H() and _S_T_O_R_E() methods, if this
  629.      is desired.
  630.  
  631.              package Mydbm;
  632.  
  633.              require SDBM_File;
  634.              require Tie::Hash;
  635.              @ISA = qw(Tie::Hash);
  636.  
  637.              sub TIEHASH {
  638.                      my $type = shift;
  639.                      my $ref = SDBM_File->new(@_);
  640.                      bless {'delegate' => $ref};
  641.              }
  642.  
  643.              sub AUTOLOAD {
  644.                      my $self = shift;
  645.  
  646.                      # The Perl interpreter places the name of the
  647.                      # message in a variable called $AUTOLOAD.
  648.  
  649.                      # DESTROY messages should never be propagated.
  650.                      return if $AUTOLOAD =~ /::DESTROY$/;
  651.  
  652.                      # Remove the package name.
  653.                      $AUTOLOAD =~ s/^Mydbm:://;
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))                                                          PPPPEEEERRRRLLLLBBBBOOOOTTTT((((1111))))
  665.  
  666.  
  667.  
  668.                      # Pass the message to the delegate.
  669.                      $self->{'delegate'}->$AUTOLOAD(@_);
  670.              }
  671.  
  672.              package main;
  673.              use Fcntl qw( O_RDWR O_CREAT );
  674.  
  675.              tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640;
  676.              $foo{'bar'} = 123;
  677.              print "foo-bar = $foo{'bar'}\n";
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.